<template>
  <el-dialog class :title="'选择供货人'" append-to-body :visible.sync="dialogVisible" :close-on-click-modal="false" width="600px">
    <div class="form-box" v-loading="loading">
      <el-table
            ref="multipleTable"
            :data="supplyList"
            tooltip-effect="dark"
            style="width: 100%"
            height="600px"
            @selection-change="handleSelectionChange2"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column label="昵称" prop="nick_name"></el-table-column>
            <el-table-column prop="create_id" label="用户ID"> </el-table-column>
            <el-table-column prop="cot" label="商品数量" v-if="gameId!='qudao'"> </el-table-column>
          </el-table>
      
            <div style="height: 60px;"></div>
            <div class="center">
              <el-button @click="close()">取消</el-button>
              <el-button type="primary" @click="submitForm('form')" v-loading="loading">确认</el-button>
            </div>
    </div>
  </el-dialog>
</template>

<script>
import { accountUserGameCount } from "@/api/system/user";
import supplyConfigApi from "@/api/system/supplyConfig";
export default {
  name: "",
  data() {
    return {
      dialogVisible: false,
      loading: false,
      supplyList:[],
      selectedSupplyList:[],
      checkSuperList:[],
      gameId:'',
      superId:'',//回显时回传的供货人ids
    };
  },
  methods: {
    // 打开弹窗
    async open(gameId,superId,content,tenantId=1) {
      this.superId = superId
      this.dialogVisible = true;
      this.loading = true
      this.gameId = gameId
      if(gameId=='qudao'){
        // 渠道级
        this.getYuangongList()
      }else{
        this.getSupplyList(gameId,content,tenantId)
      }
    },
       // 获取所有员工列表
    getYuangongList() {
      supplyConfigApi
        .getAllUserList({
          userNewType: 1,
        })
        .then((res) => {
          if(res.rows&&res.rows.length){
            res.rows.forEach(r=>{
              r.create_id = r.userId
              r.nick_name = r.nickName
            })
          }
          // this.yuangongList = res.rows;
          this.loading = false
          this.supplyList = res.rows;
          console.log("%c Line:73 🍧 this.supplyList", "color:#fca650", this.supplyList);
          this.$nextTick(()=>{
            this.toggleSelection()
          })
        });
    },
    // 查询供货商
    getSupplyList(gameId,content,tenantId) {
      accountUserGameCount({gameId,content,tenantId}).then((res) => {
        if (res.code == 200) {
          this.loading = false
          this.supplyList = res.data;
          this.$nextTick(()=>{
            this.toggleSelection()
          })
        }
      });
    },
    // 回显时勾选
    toggleSelection() {
      this.selectedSupplyList = []
      if(this.superId){
        let arr = this.superId.split(',')
        arr.forEach(row => {
            this.supplyList.forEach(r=>{
              if(r.create_id==row){
                this.$refs.multipleTable.toggleRowSelection(r);
                // this.selectedSupplyList.push(r)
              }
            })
          });
      } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
 //  选择供货商家
 handleSelectionChange2(e) {
      this.selectedSupplyList = e;
    },
    //   点击昵称显示商品详细数量
    getGoodsCont(row) {
      this.$refs.goodsCotDialog.open(row);
    },
    // 重置
    reset() {
     
    },
    submitForm() {
      let obj = {
        gameId:this.gameId,
        selectedSupplyList:JSON.stringify(this.selectedSupplyList)
      }
      this.$emit('change',obj)
      this.dialogVisible = false
      this.selectedSupplyList = []
      this.checkSuperList = []

    },
    close() {
    
      this.dialogVisible = false;
    },
  }
};
</script>
<style scoped lang="scss">
.center {
  width: 100%;
  background: #fff;
  padding: 10px 0;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  z-index: 99;
}
</style>
